use sqlx::PgPool;

use crate::base::errors::AppError;

pub struct MenuRepository;

impl MenuRepository {
    pub async fn find_menu_code_by_user_id(db_pool: &PgPool, user_id: &i64) -> Result<Vec<String>, AppError> {
        sqlx::query_scalar(
            r#"
            SELECT
                code 
            FROM
                menu
                M INNER JOIN role_menu rm ON M."id" = rm.menu_id
                INNER JOIN user_roles ur ON rm.role_id = ur.role_id 
            WHERE
                ur.user_id = $1
            "#
        )
        .bind(user_id)
        .fetch_all(db_pool)
        .await
        .map_err(|e| AppError::Database(format!("根据用户ID查询菜编码出错: {}", e)))
    }
}

